//
//  BCMHelper.m
//  通用框架
//
//  Created by 马腾威 on 16/6/30.
//  Copyright © 2016年 马腾威. All rights reserved.
//

#import "BCMHelper.h"

@implementation BCMHelper
//计算文本高度
+ (CGFloat)heightOfConttent:(NSString*)content fontName:(NSString *)fontName fontSize:(CGFloat)fontSize maxWidth:(CGFloat)maxWidth
{
    // 计算包含文本的最小边框的大小
    //1、指定最大的大小，主要指定最大宽度2、如何计算NSStringDrawingUsesLineFragmentOrigin从左上角开始计算NSStringDrawingUsesFontLeading是包含行间距
    if (content == nil || [content isEqual:[NSNull null]]) {
        return 0.0;
    }
    NSDictionary *attrs = [NSDictionary new];
    if ([fontName isEqualToString:@"System"]) {
        attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
    }else{
        attrs = @{NSFontAttributeName : [UIFont fontWithName:fontName size:fontSize]};
    }
    return [content boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.height;
    
}
//计算文本宽度
+ (CGFloat)widthOfConttent:(NSString*)content fontName:(NSString *)fontName fontSize:(CGFloat)fontSize maxHeight:(CGFloat)maxHeight
{
    // 计算包含文本的最小边框的大小
    //1、指定最大的大小，主要指定最大宽度2、如何计算NSStringDrawingUsesLineFragmentOrigin从左上角开始计算NSStringDrawingUsesFontLeading是包含行间距
    if (content.length==0) {
        return 0.0;
    }
    NSDictionary *attrs = [NSDictionary new];
    if ([fontName isEqualToString:@"System"]) {
        attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
    }else{
        attrs = @{NSFontAttributeName : [UIFont fontWithName:fontName size:fontSize]};
    }
    return [content boundingRectWithSize:CGSizeMake(MAXFLOAT,maxHeight) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attrs context:nil].size.width;
    
}
//计算不失真的图片高度
+ (CGSize)calculteImageSizeOrignilWidth:(NSString*)orignilWidth orignilHeight:(NSString*)orignilHeight needWidth:(CGFloat)width
{
    if ([orignilHeight floatValue] == 0||[orignilWidth floatValue] == 0) {
        return CGSizeZero;
    }
    // 的到在x轴上得缩放比例
    CGFloat scaleX = width/[orignilWidth floatValue];
    // 根据缩放比例的到图片的高度
    CGFloat imageHeight = [orignilHeight floatValue]*scaleX;
    //NSLog(@"imageHeight %f",imageHeight);
    return CGSizeMake(width, imageHeight);
}

//16进制转RGB 字符串举例：@“#FF0000”
+ (UIColor*)converFromStringColorToRGBColor:(NSString*)stringColor{
    // 转换成标准16进制数字符串
    NSMutableString *mStr = [NSMutableString stringWithString:stringColor];
    [mStr replaceCharactersInRange:[mStr rangeOfString:@"#" ] withString:@"0x"];
    // 十六进制字符串转成整形。
    long colorLong = strtoul([mStr cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
    // 通过位与方法获取三色值
    int R = (colorLong & 0xFF0000 )>>16;
    int G = (colorLong & 0x00FF00 )>>8;
    int B =  colorLong & 0x0000FF;
    //string转color
    UIColor *wordColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0];
    return wordColor;
}

//时间类型转字符串
+ (NSString *)dateChangeToString:(NSDate*)date{
    NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:date];
}
//字符串类型转时间类型
+ (NSDate*)stringChangeToDate:(NSString*)string{
    NSDateFormatter * formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter dateFromString:string];
}
/**
 *  传入image和size 获得切割后的图片
 *
 *  @param image 原图
 *  @param size  切割的size
 *
 *  @return 切割后的图片
 */
+ (UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size{
    
    UIGraphicsBeginImageContext(size);//size为CGSize类型，即你所需要的图片尺寸
    
    [image drawInRect:CGRectMake(0,0, size.width, size.height)];
    
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;
}
#pragma mark - 正则匹配手机号 -
+ (BOOL)checkPhoneNum:(NSString *)phoneNum
{
    //模糊匹配：开头为1的11位数字即可
//    NSString *phoneNumRegex = @"^[1]([0-9][0-9]{1}|59|58|88|89)[0-9]{8}$";
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneNumRegex];
//    BOOL isMatch = [pred evaluateWithObject:phoneNum];
//    return isMatch;
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:phoneNum];
}
#pragma mark -  正则匹配设备号 -
+ (BOOL)checkDeviceNum:(NSString *)deviceNum
{
    //纯数字 不限制长度
    NSString *deviceNumRegex = @"^[0-9]*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",deviceNumRegex];
    BOOL isMatch = [pred evaluateWithObject:deviceNum];
    return isMatch;
}
#pragma mark - 正则匹配身份证号 -
+ (BOOL)checkIDCardNum:(NSString *)IDCardNum{
    //身份证
    NSString *cardNumRegex = @"^\\d{15}|\\d{18}\([0-9]){7,18}(x|X)?\\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",cardNumRegex];
    BOOL isMatch = [pred evaluateWithObject:IDCardNum];
    return isMatch;
}
#pragma mark - 正则匹配邮箱 -
+ (BOOL)checkEmail:(NSString *)Email{
    //邮箱
    NSString *emailRegex = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
    BOOL isMatch = [pred evaluateWithObject:Email];
    return isMatch;
}
//通过日期拿到星期
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

// 通过日期拿到月份
+ (NSString *)monthSringFromDate:(NSDate *)inputDate {

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;
    [dateFormatter setDateFormat:@"yyyy-MM"];
    return [dateFormatter stringFromDate:inputDate];
}

//获取N个月前跟N个月后的日期
+(NSString *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:mDate];
}

/**
 *  字符串判空
 *
 *  @param string 传入的NSString
 *
 *  @return YES、NO
 */
+ (BOOL)isBlankString:(NSString *)string{
    string=[NSString stringWithFormat:@"%@",string];
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    if (string == nil) {
        
        return YES;
        
    }
    
    if (string == NULL) {
        
        return YES;
        
    }
    
    if([string isEqualToString:@"(null)"]){
        
        return YES;
    }
    
    if([string isEqualToString:@"<null>"]){
        
        return YES;
    }
    if(string.length == 0){
        return YES;
    }
    
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        return YES;
        
    }
    
    return NO;
    
}
@end
